package cn.liutao.common.utils;

import cn.hutool.core.map.MapUtil;
import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.http.HttpStatus;
import cn.liutao.common.entity.ResultMap;
import cn.liutao.common.enums.CommonEnum;
import cn.liutao.common.exception.OrderException;
import com.alibaba.fastjson2.JSONObject;
import com.mybatisflex.core.MybatisFlexBootstrap;
import com.mybatisflex.core.paginate.Page;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liutao
 * @Description 统一处理响应
 * @date 2023/10/30 20:12
 * @Version 1.0
 */
public class ServletUtils {

    /**
     * 获取查询参数
     * @param request
     * @param tClass
     * @return
     * @param <T>
     */
    public static<T> T getQueryParam(HttpServletRequest request,Class<T> tClass){
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        Map<String, String> paramMap = ServletUtil.getParamMap(request);
        Map map = new LinkedHashMap();
        map.put("data",paramMap);
        return MapUtil.get(map,"data",tClass);
    }

    public static List<String> getQueryParams(HttpServletRequest request,String key){
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        Map<String, String[]> params = ServletUtil.getParams(request);
        String[] strings = params.get(key);
        return Arrays.asList(strings);
    }


    /**
     * 获取body中的参数
     * @param request
     * @param tClass
     * @return
     * @param <T>
     */
    public static<T> T getBodyParam(HttpServletRequest request,Class<T> tClass){
        String body = ServletUtil.getBody(request);
        T result = JSONObject.parseObject(body, tClass);
        return result;
    }


    /**
     * 从请求中获取token
     * @param req
     * @return
     */
    public static String getToken(HttpServletRequest req) {
        String header = req.getHeader(CommonEnum.JWT_TOKEN_HEADER.getValue());
        String[] split = header.split(" ");
        String token = split[1];
        return token;
    }

    public static<T> T getMapper(Class<T> tClass){
        return MybatisFlexBootstrap.getInstance().getMapper(tClass);
    }

    public static<T> void ok(HttpServletResponse response,T data){
        try {
            ResultMap<T> resultMap = new ResultMap<>(data, "操作成功", HttpStatus.HTTP_OK);
            response.setContentType("application/json;charset=UTF-8");
            PrintWriter writer = response.getWriter();
            String jsonString = JSONObject.toJSONString(resultMap);
            writer.append(jsonString);
            writer.close();
        } catch (IOException e) {
            throw new OrderException("系统异常");
        }
    }

    public static<T> void ok(HttpServletResponse response,T data,String msg){
        try {
            ResultMap<T> resultMap = new ResultMap<>(data, msg, HttpStatus.HTTP_OK);
            response.setContentType("application/json;charset=UTF-8");
            PrintWriter writer = response.getWriter();
            String jsonString = JSONObject.toJSONString(resultMap);
            writer.append(jsonString);
            writer.close();
        } catch (IOException e) {
            throw new OrderException("系统异常");
        }
    }

    public static<T> void ok(HttpServletResponse response,Page<T> page,String msg){
        try {
            ResultMap<T> resultMap = new ResultMap<>(page, msg, HttpStatus.HTTP_OK);
            response.setContentType("application/json;charset=UTF-8");
            PrintWriter writer = response.getWriter();
            String jsonString = JSONObject.toJSONString(resultMap);
            writer.append(jsonString);
            writer.close();
        } catch (IOException e) {
            throw new OrderException("系统异常");
        }
    }

    public static<T> void ok(HttpServletResponse response,Page<T> page){
        try {
            ResultMap<T> resultMap = new ResultMap<>( page,"操作成功", HttpStatus.HTTP_OK);
            response.setContentType("application/json;charset=UTF-8");
            PrintWriter writer = response.getWriter();
            String jsonString = JSONObject.toJSONString(resultMap);
            writer.append(jsonString);
            writer.close();
        } catch (IOException e) {
            throw new OrderException("系统异常");
        }
    }

    public static<T> void ok(HttpServletResponse response,String msg){
        try {
            ResultMap<T> resultMap = new ResultMap(msg, HttpStatus.HTTP_OK);
            response.setContentType("application/json;charset=UTF-8");
            PrintWriter writer = response.getWriter();
            String jsonString = JSONObject.toJSONString(resultMap);
            writer.append(jsonString);
            writer.close();
        } catch (IOException e) {
            throw new OrderException("系统异常");
        }
    }

    public static<T> void ok(HttpServletResponse response){
        try {
            ResultMap<T> resultMap = new ResultMap<>("操作成功", HttpStatus.HTTP_OK);
            response.setContentType("application/json;charset=UTF-8");
            PrintWriter writer = response.getWriter();
            String jsonString = JSONObject.toJSONString(resultMap);
            writer.append(jsonString);
            writer.close();
        } catch (IOException e) {
            throw new OrderException("系统异常");
        }
    }

    public static<T> void error(HttpServletResponse response){
        try {
            ResultMap<T> resultMap = new ResultMap<>("操作失败", HttpStatus.HTTP_INTERNAL_ERROR);
            response.setContentType("application/json;charset=UTF-8");
            PrintWriter writer = response.getWriter();
            response.setStatus(HttpStatus.HTTP_INTERNAL_ERROR);
            String jsonString = JSONObject.toJSONString(resultMap);
            writer.append(jsonString);
            writer.close();
        } catch (IOException e) {
            throw new OrderException("系统异常");
        }
    }

    public static<T> void error(HttpServletResponse response,String msg){
        try {
            ResultMap<T> resultMap = new ResultMap<>(msg, HttpStatus.HTTP_INTERNAL_ERROR);
            response.setContentType("application/json;charset=UTF-8");
            PrintWriter writer = response.getWriter();
            response.setStatus(HttpStatus.HTTP_INTERNAL_ERROR);
            String jsonString = JSONObject.toJSONString(resultMap);
            writer.append(jsonString);
            writer.close();
        } catch (IOException e) {
            throw new OrderException("系统异常");
        }
    }

    public static<T> void error(HttpServletResponse response,String msg,int code){
        try {
            ResultMap<T> resultMap = new ResultMap<>(msg,code);
            response.setContentType("application/json;charset=UTF-8");
            PrintWriter writer = response.getWriter();
            if (code==HttpStatus.HTTP_BAD_REQUEST)response.setStatus(HttpStatus.HTTP_OK);
            else response.setStatus(code);
            String jsonString = JSONObject.toJSONString(resultMap);
            writer.append(jsonString);
            writer.close();
        } catch (IOException e) {
            throw new OrderException("系统异常");
        }
    }



}
